﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DAWSharp.UI.WinFormsControls
{
    public partial class Pot : UserControl
    {
        private double minimum = 0.0;
        private double maximum = 1.0;
        private double Value = 0.5;
        private int beginDragY;
        private double beginDragValue;
        private bool dragging;
        public int ValuePercent { get { return (int)(Value * 100); } }
        public float ValueFloat { get { return (float)Value; } }
        public float ValueDB { get { return 20.0f * (float)Math.Log10((float)Value / 1.0f); } }

        public Pot()
        {
            this.SetStyle(ControlStyles.DoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint, true);
            InitializeComponent();
        }

        public override string ToString()
        {
            return string.Format(@"{3} values: {0}% | {1}f | {2}dB", ValuePercent.ToString(), ValueFloat.ToString(), ValueDB.ToString(), this.Name);
        }

        private void SetValue(double newValue)
        {
            if (this.Value != newValue)
            {
                this.Value = newValue;
                Console.WriteLine(this.ToString());
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int diameter = Math.Min(this.Width - 4, this.Height - 4);

            Pen potPen = new Pen(ForeColor, 3.0f);
            potPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
            System.Drawing.Drawing2D.GraphicsState state = e.Graphics.Save();
            e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            double percent = (Value - minimum) / (maximum - minimum);
            double degrees = 135 + (percent * 270);
            double x = (diameter / 2.0) * Math.Cos(Math.PI * degrees / 180);
            double y = (diameter / 2.0) * Math.Sin(Math.PI * degrees / 180);
            e.Graphics.DrawLine(potPen, 0, 0, (float)x, (float)y);
            e.Graphics.Restore(state);
            base.OnPaint(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            dragging = true;
            beginDragY = e.Y;
            beginDragValue = Value;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            dragging = false;
            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (dragging)
            {
                int yDifference = beginDragY - e.Y;
                double delta = (maximum - minimum) * (yDifference / 150.0);
                double newValue = beginDragValue + delta;
                if (newValue < minimum)
                    newValue = minimum;
                if (newValue > maximum)
                    newValue = maximum;
                SetValue(newValue);
            }
            base.OnMouseMove(e);
        }
    }
}
